- if params[:period]
  = main_toolbar do |t|
    = dropdown_menu_button(:export.ta, icon: :download) do |d|
      - export_params = params.slice(:started_on, :stopped_on, :period, :states, :accounts, :centralize, :level_1, :level_2, :level_3, :level_4, :level_5, :level_6, :level_7, :level_8, :level_9, :level_10)
      - d.item :export_as_ods.tl, params: export_params.merge(format: :ods)
      - d.item :export_as_csv.tl, params: export_params.merge(format: :csv)
      - d.item :export_as_xcsv.tl, params: export_params.merge(format: :xcsv)

= kujaku do |k|
  - k.helper :journal_period_crit
  - k.helper :journal_entries_states_crit
  - k.helper :accounts_range_crit
  - k.helper do
    - centralized = Preference.order("id").where("name LIKE ? ", "third_%_accounts").collect{ |p| p.value.to_s }.join(" ")
    %label= :centralize.tl
    = text_field_tag("centralize", params[:centralize]||centralized, :size => 30)
  - k.helper do
    - max = Account.maximum("LENGTH(TRIM(number))").to_i
    %label= :subtotals_at_level.tl
    - for level in 1..max-1
      - name = "level_#{level}"
      = check_box_tag(name, "1", params[name]=="1")
      %label{:for => name}= level

- if @balance
  - currency = Preference[:currency]
  .active-list
    %table.list
      %thead
        %tr
          %th{rowspan: 2}= JournalEntryItem.human_attribute_name(:account_number)
          %th{rowspan: 2}= JournalEntryItem.human_attribute_name(:account_name)
          %th{colspan: 2}= :total.tl
          %th{colspan: 2}= :balance.tl
        %tr
          %th= JournalEntry.human_attribute_name(:debit)
          %th= JournalEntry.human_attribute_name(:credit)
          %th= JournalEntry.human_attribute_name(:debit)
          %th= JournalEntry.human_attribute_name(:credit)
      - for item in @balance
        - d = item[0].size-1
        - if item[1].to_i > 0
          - account = Account.find(item[1])
          %tr{class: cycle(:odd, :even)}
            %td.code= link_to(account.number, action: :show, controller: :accounts, id: account.id)
            %td= link_to(account.name, action: :show, controller: :accounts, id: account.id)
            %td.dec= number_to_accountancy(item[2], currency)
            %td.dec= number_to_accountancy(item[3], currency)
            %td.dec= number_to_accountancy(item[4], currency) if item[4].to_f > 0
            %td.dec= number_to_accountancy(-item[4].to_f, currency) if item[4].to_f < 0
        - elsif item[1].to_i == -1
          %tr.total
            %th{colspan: 2}= :total.tl
            %td.dec= number_to_accountancy(item[2], currency)
            %td.dec= number_to_accountancy(item[3], currency)
            %td.dec= number_to_accountancy(item[4], currency) if item[4].to_f > 0
            %td.dec= number_to_accountancy(-item[4].to_f, currency) if item[4].to_f < 0
        - elsif item[1].to_i == -2
          %tr.subtotal
            %th{colspan: 2, style: "padding-left: #{d*8+4}px"}= :subtotal.tl(name: item[0])
            %td.dec= number_to_accountancy(item[2], currency)
            %td.dec= number_to_accountancy(item[3], currency)
            %td.dec= number_to_accountancy(item[4], currency) if item[4].to_f > 0
            %td.dec= number_to_accountancy(-item[4].to_f, currency) if item[4].to_f < 0
        - elsif item[1].to_i == -3
          %tr.centralize{class: cycle(:odd, :even)}
            %td= item[0]
            %td= :centralized_account.tl(name: item[0])
            %td.dec= number_to_accountancy(item[2], currency)
            %td.dec= number_to_accountancy(item[3], currency)
            %td.dec= number_to_accountancy(item[4], currency) if item[4].to_f > 0
            %td.dec= number_to_accountancy(-item[4].to_f, currency) if item[4].to_f < 0
.end
